{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Tensorflow Linear Regression Single Feature Part 2"
      ],
      "metadata": {}
    },
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "\n",
        "import warnings\n",
        "warnings.filterwarnings(\"ignore\")\n",
        "\n",
        "# yahoo finance is used to fetch data \n",
        "import yfinance as yf\n",
        "yf.pdr_override()"
      ],
      "outputs": [],
      "execution_count": 1,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:10:59.292Z",
          "iopub.execute_input": "2020-07-26T21:10:59.302Z",
          "shell.execute_reply": "2020-07-26T21:11:00.391Z",
          "iopub.status.idle": "2020-07-26T21:11:00.357Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# input\n",
        "symbol = 'AMD'\n",
        "start = '2007-01-01'\n",
        "end = '2018-11-01'\n",
        "\n",
        "# Read data \n",
        "dataset = yf.download(symbol,start,end)\n",
        "\n",
        "# View Columns\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[*********************100%***********************]  1 of 1 completed\n"
          ]
        },
        {
          "output_type": "execute_result",
          "execution_count": 2,
          "data": {
            "text/plain": "            Adj Close      Close       High        Low       Open    Volume\nDate                                                                       \n2007-01-03  19.520000  19.520000  20.400000  19.350000  20.080000  28350300\n2007-01-04  19.790001  19.790001  19.860001  19.320000  19.660000  23652500\n2007-01-05  19.709999  19.709999  19.910000  19.540001  19.540001  15902400\n2007-01-08  19.469999  19.469999  19.860001  19.370001  19.709999  15814800\n2007-01-09  19.650000  19.650000  19.709999  19.370001  19.450001  14494200",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Adj Close</th>\n      <th>Close</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Open</th>\n      <th>Volume</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2007-01-03</th>\n      <td>19.520000</td>\n      <td>19.520000</td>\n      <td>20.400000</td>\n      <td>19.350000</td>\n      <td>20.080000</td>\n      <td>28350300</td>\n    </tr>\n    <tr>\n      <th>2007-01-04</th>\n      <td>19.790001</td>\n      <td>19.790001</td>\n      <td>19.860001</td>\n      <td>19.320000</td>\n      <td>19.660000</td>\n      <td>23652500</td>\n    </tr>\n    <tr>\n      <th>2007-01-05</th>\n      <td>19.709999</td>\n      <td>19.709999</td>\n      <td>19.910000</td>\n      <td>19.540001</td>\n      <td>19.540001</td>\n      <td>15902400</td>\n    </tr>\n    <tr>\n      <th>2007-01-08</th>\n      <td>19.469999</td>\n      <td>19.469999</td>\n      <td>19.860001</td>\n      <td>19.370001</td>\n      <td>19.709999</td>\n      <td>15814800</td>\n    </tr>\n    <tr>\n      <th>2007-01-09</th>\n      <td>19.650000</td>\n      <td>19.650000</td>\n      <td>19.709999</td>\n      <td>19.370001</td>\n      <td>19.450001</td>\n      <td>14494200</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 2,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:00.369Z",
          "iopub.execute_input": "2020-07-26T21:11:00.377Z",
          "iopub.status.idle": "2020-07-26T21:11:01.777Z",
          "shell.execute_reply": "2020-07-26T21:11:01.801Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset['Increase_Decrease'] = np.where(dataset['Volume'].shift(-1) > dataset['Volume'],1,0)\n",
        "dataset['Buy_Sell_on_Open'] = np.where(dataset['Open'].shift(-1) > dataset['Open'],1,0)\n",
        "dataset['Buy_Sell'] = np.where(dataset['Adj Close'].shift(-1) > dataset['Adj Close'],1,0)\n",
        "dataset['Returns'] = dataset['Adj Close'].pct_change()\n",
        "dataset = dataset.dropna()\n",
        "dataset.head()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 3,
          "data": {
            "text/plain": "            Adj Close      Close       High        Low       Open    Volume  \\\nDate                                                                          \n2007-01-04  19.790001  19.790001  19.860001  19.320000  19.660000  23652500   \n2007-01-05  19.709999  19.709999  19.910000  19.540001  19.540001  15902400   \n2007-01-08  19.469999  19.469999  19.860001  19.370001  19.709999  15814800   \n2007-01-09  19.650000  19.650000  19.709999  19.370001  19.450001  14494200   \n2007-01-10  20.010000  20.010000  20.020000  19.500000  19.639999  19783200   \n\n            Increase_Decrease  Buy_Sell_on_Open  Buy_Sell   Returns  \nDate                                                                 \n2007-01-04                  0                 0         0  0.013832  \n2007-01-05                  0                 1         0 -0.004043  \n2007-01-08                  0                 0         1 -0.012177  \n2007-01-09                  1                 1         1  0.009245  \n2007-01-10                  1                 1         1  0.018321  ",
            "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Adj Close</th>\n      <th>Close</th>\n      <th>High</th>\n      <th>Low</th>\n      <th>Open</th>\n      <th>Volume</th>\n      <th>Increase_Decrease</th>\n      <th>Buy_Sell_on_Open</th>\n      <th>Buy_Sell</th>\n      <th>Returns</th>\n    </tr>\n    <tr>\n      <th>Date</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2007-01-04</th>\n      <td>19.790001</td>\n      <td>19.790001</td>\n      <td>19.860001</td>\n      <td>19.320000</td>\n      <td>19.660000</td>\n      <td>23652500</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.013832</td>\n    </tr>\n    <tr>\n      <th>2007-01-05</th>\n      <td>19.709999</td>\n      <td>19.709999</td>\n      <td>19.910000</td>\n      <td>19.540001</td>\n      <td>19.540001</td>\n      <td>15902400</td>\n      <td>0</td>\n      <td>1</td>\n      <td>0</td>\n      <td>-0.004043</td>\n    </tr>\n    <tr>\n      <th>2007-01-08</th>\n      <td>19.469999</td>\n      <td>19.469999</td>\n      <td>19.860001</td>\n      <td>19.370001</td>\n      <td>19.709999</td>\n      <td>15814800</td>\n      <td>0</td>\n      <td>0</td>\n      <td>1</td>\n      <td>-0.012177</td>\n    </tr>\n    <tr>\n      <th>2007-01-09</th>\n      <td>19.650000</td>\n      <td>19.650000</td>\n      <td>19.709999</td>\n      <td>19.370001</td>\n      <td>19.450001</td>\n      <td>14494200</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.009245</td>\n    </tr>\n    <tr>\n      <th>2007-01-10</th>\n      <td>20.010000</td>\n      <td>20.010000</td>\n      <td>20.020000</td>\n      <td>19.500000</td>\n      <td>19.639999</td>\n      <td>19783200</td>\n      <td>1</td>\n      <td>1</td>\n      <td>1</td>\n      <td>0.018321</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
          },
          "metadata": {}
        }
      ],
      "execution_count": 3,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:01.829Z",
          "iopub.execute_input": "2020-07-26T21:11:01.837Z",
          "iopub.status.idle": "2020-07-26T21:11:01.869Z",
          "shell.execute_reply": "2020-07-26T21:11:01.974Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "dataset.shape"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 4,
          "data": {
            "text/plain": "(2979, 10)"
          },
          "metadata": {}
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:01.888Z",
          "iopub.execute_input": "2020-07-26T21:11:01.897Z",
          "iopub.status.idle": "2020-07-26T21:11:01.920Z",
          "shell.execute_reply": "2020-07-26T21:11:04.282Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "print(tf.__version__)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1.10.0\n"
          ]
        }
      ],
      "execution_count": 5,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:01.934Z",
          "iopub.execute_input": "2020-07-26T21:11:01.943Z",
          "iopub.status.idle": "2020-07-26T21:11:03.659Z",
          "shell.execute_reply": "2020-07-26T21:11:04.289Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "feature = np.array(dataset['Returns'])\n",
        "target = np.array(dataset['Adj Close'])"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:03.672Z",
          "iopub.execute_input": "2020-07-26T21:11:03.683Z",
          "iopub.status.idle": "2020-07-26T21:11:03.698Z",
          "shell.execute_reply": "2020-07-26T21:11:04.297Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from sklearn.model_selection import train_test_split  \n",
        "X_train, X_test, Y_train, Y_test = train_test_split(feature, target, test_size = 1/4, random_state = 0)"
      ],
      "outputs": [],
      "execution_count": 7,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:03.712Z",
          "iopub.execute_input": "2020-07-26T21:11:03.720Z",
          "iopub.status.idle": "2020-07-26T21:11:04.327Z",
          "shell.execute_reply": "2020-07-26T21:11:04.303Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# TF graph input\n",
        "X = tf.placeholder(tf.float32, name='X')\n",
        "Y = tf.placeholder(tf.float32, name='Y')"
      ],
      "outputs": [],
      "execution_count": 8,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.345Z",
          "iopub.execute_input": "2020-07-26T21:11:04.356Z",
          "iopub.status.idle": "2020-07-26T21:11:04.366Z",
          "shell.execute_reply": "2020-07-26T21:11:04.310Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Set model weights\n",
        "w = tf.get_variable('weights', initializer=tf.constant(0.0))\n",
        "b = tf.get_variable('bias', initializer=tf.constant(0.0))"
      ],
      "outputs": [],
      "execution_count": 9,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.381Z",
          "iopub.execute_input": "2020-07-26T21:11:04.393Z",
          "iopub.status.idle": "2020-07-26T21:11:04.410Z",
          "shell.execute_reply": "2020-07-26T21:11:04.821Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Set parameters\n",
        "learning_rate = 0.1\n",
        "training_iteration = 200\n",
        "# Construct a linear model\n",
        "model = X * w + b"
      ],
      "outputs": [],
      "execution_count": 10,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.427Z",
          "iopub.execute_input": "2020-07-26T21:11:04.438Z",
          "iopub.status.idle": "2020-07-26T21:11:04.453Z",
          "shell.execute_reply": "2020-07-26T21:11:04.828Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "samples_number = feature.size"
      ],
      "outputs": [],
      "execution_count": 11,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.469Z",
          "iopub.execute_input": "2020-07-26T21:11:04.482Z",
          "iopub.status.idle": "2020-07-26T21:11:04.496Z",
          "shell.execute_reply": "2020-07-26T21:11:04.833Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Minimize squared errors\n",
        "cost_function = tf.reduce_sum(tf.pow(model - Y, 2))/(2 * samples_number) #L2 loss\n",
        "optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function) #Gradient descent"
      ],
      "outputs": [],
      "execution_count": 12,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.510Z",
          "iopub.execute_input": "2020-07-26T21:11:04.520Z",
          "iopub.status.idle": "2020-07-26T21:11:04.538Z",
          "shell.execute_reply": "2020-07-26T21:11:04.837Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Initialize variables\n",
        "init = tf.global_variables_initializer()"
      ],
      "outputs": [],
      "execution_count": 13,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.558Z",
          "iopub.execute_input": "2020-07-26T21:11:04.572Z",
          "iopub.status.idle": "2020-07-26T21:11:04.591Z",
          "shell.execute_reply": "2020-07-26T21:11:04.844Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sess=tf.Session()\n",
        "sess.run(init)"
      ],
      "outputs": [],
      "execution_count": 14,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.608Z",
          "iopub.execute_input": "2020-07-26T21:11:04.619Z",
          "iopub.status.idle": "2020-07-26T21:11:04.639Z",
          "shell.execute_reply": "2020-07-26T21:11:04.849Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sess.run(optimizer,{X:X_test,Y:Y_test})"
      ],
      "outputs": [],
      "execution_count": 15,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.656Z",
          "iopub.execute_input": "2020-07-26T21:11:04.663Z",
          "iopub.status.idle": "2020-07-26T21:11:04.677Z",
          "shell.execute_reply": "2020-07-26T21:11:04.854Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sess.run([w,b])"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 16,
          "data": {
            "text/plain": "[0.0004692331, 0.17247967]"
          },
          "metadata": {}
        }
      ],
      "execution_count": 16,
      "metadata": {
        "collapsed": false,
        "outputHidden": false,
        "inputHidden": false,
        "execution": {
          "iopub.status.busy": "2020-07-26T21:11:04.692Z",
          "iopub.execute_input": "2020-07-26T21:11:04.701Z",
          "iopub.status.idle": "2020-07-26T21:11:04.724Z",
          "shell.execute_reply": "2020-07-26T21:11:04.859Z"
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "python3"
    },
    "language_info": {
      "pygments_lexer": "ipython3",
      "file_extension": ".py",
      "name": "python",
      "codemirror_mode": {
        "version": 3,
        "name": "ipython"
      },
      "nbconvert_exporter": "python",
      "mimetype": "text/x-python",
      "version": "3.5.5"
    },
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3"
    },
    "nteract": {
      "version": "0.24.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}